﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Lon.Server
{
    class DataFilter
    {
        #region DataGridView过滤器
        Dictionary<string, string> filterDict = new Dictionary<string, string>();

        public void Clear()
        {
            filterDict.Clear();
        }

        public void Set(string key, string value)
        {
            //添加新项
            if (!string.IsNullOrEmpty(value) && !filterDict.ContainsKey(key))
            {
                filterDict.Add(key, value);
                return;
            }

            //更新或删除
            if (filterDict.ContainsKey(key))
            {
                if (!string.IsNullOrEmpty(value))
                {
                    //值不为空，更新
                    filterDict[key] = value;
                }
                else
                {
                    //值为空，删除
                    filterDict.Remove(key);
                }
            }

        }

        public string GetText()
        {
            StringBuilder sb = new StringBuilder();
            string fmtLike = " {0} like '%{1}%' ";

            foreach (string key in filterDict.Keys)
            {
                sb.Append(string.Format(fmtLike, key, filterDict[key]));
                sb.Append(" AND ");
            }

            if (sb.Length > 1)
            {
                sb.Remove(sb.Length - 5, 5);
            }

            return sb.ToString();

        }

        #endregion
    }


}
